import * as applicationService from '../../services/organize/applicationService';
import { message } from "antd";
import config from '../../config/config';
import * as commonUtil from '../../utils/commonUtil';

/**
 * 单位管理Model
 */
export default {
  namespace: "applicationModel",
  state: {
    applicationLoading: false,
    applicationList: [],
    total: 0,
    currentPage: 0,
    pageSize: 0,
    applicationModalVisible: false,
    operateType: 'add',
    applicationRecord: null,
    selectedRowKeys: [],
    searchParams: null,
    unfoldCollapseKeys: [],
    radioValue: "application",
  },

  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    },
  },

  effects: {
    *getListDatas({payload: {currentPage = 1, pageSize = config.PAGE_SIZE, params}}, { select, call, put }) {
      yield put({ type: "updateState", payload: { applicationLoading: true }});
      const res = yield call(applicationService.getApplicationPage, { ...params, currentPage, pageSize });
      if (res.code == "200") {
        yield put({
          type: 'updateState',
          payload: { applicationList: res.data.records, total: res.data.total, currentPage, pageSize },
        });
      }
      yield put({ type: "updateState", payload: { applicationLoading: false }});
    },

    *addApplication({payload: params}, {put, call}) {
      const res = yield call(applicationService.addApplication, params);
      if (res.code == "200") {
        message.info("新增成功！");
        yield put({ type: 'updateState', payload: { applicationModalVisible: false }});
        yield put({ type: 'getListDatas', payload: {}});
      } else {
        message.info("新增失败！");
      }
    },

    *updateApplication({payload: params}, {put, call}) {
      const res = yield call(applicationService.updateApplication, params);
      if (res.code == "200") {
        message.info("修改成功！");
        yield put({ type: 'updateState', payload: { applicationModalVisible: false }});
        yield put({ type: 'getListDatas', payload: {}});
      } else {
        message.info("修改失败！");
      }
    },

    *deleteApplication({ payload: params }, {put, call}) {
      const res = yield call(applicationService.deleteApplication, params);
      if (res.code == "200") {
        message.info("删除成功！");
        yield put({ type: 'getListDatas', payload: {}});
      } else {
        message.info("删除失败！");
      }
    },

    *unfoldCollapse({payload: params}, {select, call, put}) {
      const {applicationList, unfoldCollapseKeys} = yield select(state => state.applicationModel);
      if (unfoldCollapseKeys.length == 0) {
        const unfoldCollapseKeys = new Array();
        commonUtil.unfoldAllNode(applicationList, unfoldCollapseKeys);
        yield put({type: 'updateState', payload: {unfoldCollapseKeys}});
      } else {
        yield put({type: 'updateState', payload: {unfoldCollapseKeys: []}});
      }
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      const pathname = window.location.pathname;
      console.log("applicationModel subscriptions setup pathname => ", pathname);
      if (pathname === "/organize/app") {
        dispatch({ type: 'getListDatas', payload: {}});
      }
    },
  },
}
